package com.lanhao.aot.afterselling;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;

public class AfterSellingUtil {
	private static final Logger LOG = LoggerFactory.getLogger(AfterSellingUtil.class);

	public static final String BASE_URL = "http://kfapigw.iflytek.com/platForm/restPost";
	public static final String Get_Province_URL = "/aish_region/getProvinceList_rest_2705";
	public static final String Get_CITY_URL = "/aish_region/getCityList_rest_1579";
	public static final String Get_AREA_URL = "/aish_region/getAreaList_rest_6587";
	public static final String DEFAULT_TOKEN = "3";

	public static final String API_GW_URL = "http://qxbapigwtest.iflytek.com:8085/platForm/restPost/aish_exchange/api_router_0001";
//	public static final String APP_SECRET = "3ca0328e511bb0a5e22b88c5026e4ef9";
	public static final String APP_SECRET = "x4z5r18q23yi5p7a";
	public static final String ACCOUNT = "appKey4test";
	public static final String JWT_TOKEN = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsaWNlbnNlIjoibWFkZSBieSB3aXQiLCJ1c2VyX2lkIjoiMTA2NzYyOTkxNjUyNTA3MjM4NyIsInVzZXJfbmFtZSI6ImFwaXRlc3QiLCJzY29wZSI6WyJzZXJ2ZXIiXSwiZXhwIjozMTQ0ODA4MjkwLCJhdXRob3JpdGllcyI6WyJST0xFX0FQSSIsIlJPTEVfVVNFUiJdLCJqdGkiOiI3OTZhZjExNC1iMzRlLTRiOTUtYjM2MC1hNjM0NGY1MGVhMGQiLCJjbGllbnRfaWQiOiJhcGkifQ.XHCcGtpgFOpX8T9YpoxGjMasM3zo0YfB56v_lE_PgFE";
	public static final String SPLITTER = "`";

	public static void main(String[] args) {
//		getProvinceList();
//		getCityList("340000");
//		getAreaList("340100");
		
		createWorkOrder();
		
		// 
//		getWorkOrderStatus("IFLYSHWX202208230939360008");
	}

	public static String getProvinceList() {
		Map<String, String> headerMap = Maps.newHashMap();
		headerMap.put("token", DEFAULT_TOKEN);
		String respBody = HttpRequest.post(BASE_URL + Get_Province_URL).addHeaders(headerMap).setConnectionTimeout(1000)
				.setReadTimeout(3000).execute().body();
		// {"result":true,"message":"返回省数据成功","content":[{"eduMarketId":null,"provinceDesc":"上海市","provinceId":"310000"},{"eduMarketId":null,"provinceDesc":"江苏省","provinceId":"320000"},{"eduMarketId":null,"provinceDesc":"浙江省","provinceId":"330000"},{"eduMarketId":null,"provinceDesc":"安徽省","provinceId":"340000"},{"eduMarketId":null,"provinceDesc":"福建省","provinceId":"350000"},{"eduMarketId":null,"provinceDesc":"江西省","provinceId":"360000"},{"eduMarketId":null,"provinceDesc":"山东省","provinceId":"370000"},{"eduMarketId":null,"provinceDesc":"河南省","provinceId":"410000"},{"eduMarketId":null,"provinceDesc":"湖北省","provinceId":"420000"},{"eduMarketId":null,"provinceDesc":"北京市","provinceId":"110000"},{"eduMarketId":null,"provinceDesc":"天津市","provinceId":"120000"},{"eduMarketId":null,"provinceDesc":"河北省","provinceId":"130000"},{"eduMarketId":null,"provinceDesc":"山西省","provinceId":"140000"},{"eduMarketId":null,"provinceDesc":"内蒙古自治区","provinceId":"150000"},{"eduMarketId":null,"provinceDesc":"辽宁省","provinceId":"210000"},{"eduMarketId":null,"provinceDesc":"吉林省","provinceId":"220000"},{"eduMarketId":null,"provinceDesc":"黑龙江省","provinceId":"230000"},{"eduMarketId":null,"provinceDesc":"湖南省","provinceId":"430000"},{"eduMarketId":null,"provinceDesc":"广东省","provinceId":"440000"},{"eduMarketId":null,"provinceDesc":"广西壮族自治区","provinceId":"450000"},{"eduMarketId":null,"provinceDesc":"海南省","provinceId":"460000"},{"eduMarketId":null,"provinceDesc":"重庆市","provinceId":"500000"},{"eduMarketId":null,"provinceDesc":"四川省","provinceId":"510000"},{"eduMarketId":null,"provinceDesc":"贵州省","provinceId":"520000"},{"eduMarketId":null,"provinceDesc":"云南省","provinceId":"530000"},{"eduMarketId":null,"provinceDesc":"西藏自治区","provinceId":"540000"},{"eduMarketId":null,"provinceDesc":"陕西省","provinceId":"610000"},{"eduMarketId":null,"provinceDesc":"甘肃省","provinceId":"620000"},{"eduMarketId":null,"provinceDesc":"青海省","provinceId":"630000"},{"eduMarketId":null,"provinceDesc":"宁夏回族自治区","provinceId":"640000"},{"eduMarketId":null,"provinceDesc":"新疆维吾尔自治","provinceId":"650000"},{"eduMarketId":null,"provinceDesc":"台湾省","provinceId":"710000"},{"eduMarketId":null,"provinceDesc":"香港特别行政区","provinceId":"810000"},{"eduMarketId":null,"provinceDesc":"澳门特别行政区","provinceId":"820000"}]}
		LOG.info("请求获取省份地址接口，返回resp={}", respBody);
		return respBody;
	}

	public static String getCityList(String provinceId) {
		Map<String, String> headerMap = Maps.newHashMap();
		headerMap.put("token", DEFAULT_TOKEN);
		Map<String, Object> formMap = new HashMap<String, Object>();
		JSONObject param = new JSONObject();
		param.put("provinceId", provinceId);
		formMap.put("param", param.toJSONString());
		String respBody = HttpRequest.post(BASE_URL + Get_CITY_URL).addHeaders(headerMap).setConnectionTimeout(1000)
				.setReadTimeout(3000).form(formMap).execute().body();
		// {"result":true,"message":"返回市数据成功","content":[{"cityDesc":"合肥市","code":"340100","cityId":"340100","provinceId":"340000"},{"cityDesc":"芜湖市","code":"340200","cityId":"340200","provinceId":"340000"},{"cityDesc":"蚌埠市","code":"340300","cityId":"340300","provinceId":"340000"},{"cityDesc":"淮南市","code":"340400","cityId":"340400","provinceId":"340000"},{"cityDesc":"马鞍山市","code":"340500","cityId":"340500","provinceId":"340000"},{"cityDesc":"淮北市","code":"340600","cityId":"340600","provinceId":"340000"},{"cityDesc":"铜陵市","code":"340700","cityId":"340700","provinceId":"340000"},{"cityDesc":"安庆市","code":"340800","cityId":"340800","provinceId":"340000"},{"cityDesc":"黄山市","code":"341000","cityId":"341000","provinceId":"340000"},{"cityDesc":"滁州市","code":"341100","cityId":"341100","provinceId":"340000"},{"cityDesc":"阜阳市","code":"341200","cityId":"341200","provinceId":"340000"},{"cityDesc":"宿州市","code":"341300","cityId":"341300","provinceId":"340000"},{"cityDesc":"六安市","code":"341500","cityId":"341500","provinceId":"340000"},{"cityDesc":"亳州市","code":"341600","cityId":"341600","provinceId":"340000"},{"cityDesc":"池州市","code":"341700","cityId":"341700","provinceId":"340000"},{"cityDesc":"宣城市","code":"341800","cityId":"341800","provinceId":"340000"}]}
		LOG.info("请求获取城市地址接口，返回resp={}", respBody);
		return respBody;
	}

	public static String getAreaList(String cityId) {
		Map<String, String> headerMap = Maps.newHashMap();
		headerMap.put("token", DEFAULT_TOKEN);
		Map<String, Object> formMap = new HashMap<String, Object>();
		JSONObject param = new JSONObject();
		param.put("cityId", cityId);
		formMap.put("param", param.toJSONString());

		String respBody = HttpRequest.post(BASE_URL + Get_AREA_URL).addHeaders(headerMap).setConnectionTimeout(1000)
				.setReadTimeout(3000).form(formMap).execute().body();
		// {"result":true,"message":"返回区数据成功","content":[{"zipcode":null,"areaId":"340101","areaDesc":"市辖区","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340103","areaDesc":"庐阳区","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340123","areaDesc":"肥西县","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340181","areaDesc":"巢湖市","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340111","areaDesc":"包河区","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340122","areaDesc":"肥东县","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340124","areaDesc":"庐江县","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340102","areaDesc":"瑶海区","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340104","areaDesc":"蜀山区","cityId":"340100","provinceId":"340000"},{"zipcode":null,"areaId":"340121","areaDesc":"长丰县","cityId":"340100","provinceId":"340000"}]}
		LOG.info("请求获取区域地址接口，返回resp={}", respBody);
		return respBody;
	}

	public static Map<String, String> getJWTHeader() {
		HashMap<String, String> headerMap = Maps.newHashMap();
		headerMap.put("authorization", JWT_TOKEN);
		return headerMap;
	}

	public static Map<String, Object> getRequestForm(JSONObject p, String m) {
		Map<String, Object> formMap = new HashMap<String, Object>();
		JSONObject param = new JSONObject();
		param.put("a", ACCOUNT);
		param.put("m", m);
		param.put("i", System.currentTimeMillis());
		param.put("p", p);
		param.put("s", WitSecUtil.genSign(param, Lists.newArrayList(), APP_SECRET));
		formMap.put("parm", param.toJSONString());
		return formMap;
	}

	/**
	 * 返回工单号
	 * 
	 * @return
	 */
	public static String createWorkOrder() {
		JSONObject request = new JSONObject();
		request.put("source", "13"); 
		request.put("createTime", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_FORMATTER)); 
		request.put("serviceType", "4"); // 1，维修 2，巡检 3，培训 4，安装 5，咨询 6，投诉 7，变更信息
		request.put("serviceWay", "3"); // 1，寄修 2，到店（送修）3，上门 4，沟通调解
		request.put("sn", "Z061234567890123");
		request.put("category", "KKMVH1"); // 固定
		request.put("model", "F3PSFF"); // 固定
		request.put("customerName", "周昆");
		request.put("customerPhone", "18130064531");
		request.put("customerProvince", "340000");
		request.put("customerCity", "340100");
		request.put("customerRegion", "340104");
		request.put("customerAddress", "海亮九玺北区3栋");
		request.put("faultDescription", "门厚度：70mm 导向片规格：240*24mm");
		request.put("imgUrl", "https://www.itxm.cn/zb_users/theme/ydblog/include/random/29.png`https://www.itxm.cn/zb_users/theme/ydblog/include/random/30.png");

		Map<String, Object> requestForm = getRequestForm(request, "kdxf.order.common.repair");
		Map<String, String> jwtHeader = getJWTHeader();

		String respBody = HttpRequest.post(API_GW_URL).addHeaders(jwtHeader).setConnectionTimeout(1000)
				.setReadTimeout(3000).form(requestForm).execute().body();
		LOG.info("调用创建工单接口，request={}，返回结果respBody={}", requestForm, respBody);
		JSONObject respObject = JSON.parseObject(respBody);
		JSONObject contentObject = respObject.getJSONObject("content");
		String result = contentObject.getString("result");
		if ("OK".equals(result)) {
			String data = respObject.getString("data");
			return data;
		}
		String message = contentObject.getString("message");
		LOG.info("调用售后系统创建工单失败，message={}", message);
		return StringUtils.EMPTY;
	}

	public static String getWorkOrderStatus(String orderNo) {
		JSONObject request = new JSONObject();
		request.put("orderNo", orderNo);
		Map<String, Object> requestForm = getRequestForm(request, "kdxf.order.repair.status");
		Map<String, String> jwtHeader = getJWTHeader();
		String respBody = HttpRequest.post(API_GW_URL).addHeaders(jwtHeader).setConnectionTimeout(1000)
				.setReadTimeout(3000).form(requestForm).execute().body();
		LOG.info("调用查询工单状态接口，request={}，返回结果respBody={}", requestForm, respBody);
		JSONObject respObject = JSON.parseObject(respBody);
		JSONObject contentObject = respObject.getJSONObject("content");
		String result = contentObject.getString("result");
		if ("OK".equals(result)) {
			JSONObject dataObject = contentObject.getJSONObject("data");
			String state = dataObject.getString("state");
			return state;
		}
		String message = contentObject.getString("message");
		LOG.info("调用售后系统创建工单失败，message={}", message);
		return StringUtils.EMPTY;
	}

}
